package in.muton.panels.paginator.badgehelpers.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.components.messaging.api.UserMessage;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UnivBadgeRoles extends BadgeRoles {
	private static final long serialVersionUID = 2574930603925101484L;
	private static final Log log = LogFactory.getLog(UnivBadgeRoles.class);

	public UnivBadgeRoles(IModel<Universities> model) {
		this.univModel = model;
		roleMap = new HashMap<String, Integer>();
		roleMap.put("Administrator", UserRoles.ADMIN_ROLE);
		roleMap.put("Faculty", UserRoles.TEACHER_ROLE);
		roleMap.put("Student", UserRoles.STUDENT_ROLE);
	}

	@Override
	public String changeRole(Users user, String role) {
		if(isLastAdmin(user))
			return "Cannot change role for "+ user.getCompleteUserName()+
			" as this user is the last administrator for the university";
		
		
		String message = "The role of " + user.getCompleteUserName()
				+ " in the university "
				+ univModel.getObject().getUniversityName()
				+ " has been changed to " + role + 
				(getRole(user).equals("")?"":" from " + getRole(user));

		UserRolesDAO ur = new UserRolesDAO(EduShareSession.get()
				.getSessionORMInterface());
		ur.changeRole(univModel.getObject(), user, roleMap.get(role));

		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new UnivDataHandler(univModel).getAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				univModel.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}

	@Override
	public List<String> getOtherRoles(Users user) {
		List<String> rList = new ArrayList<String>();
		int role = UnivRoleCheck.getRole(univModel.getObject(), user);
		for (String key : roleMap.keySet()) {
			if (roleMap.get(key) != role)
				rList.add(key);
		}
		log.debug("Size of other roles is " + rList.size());
		return rList;
	}

	@Override
	public String removeUser(Users user) {
		if (isLastAdmin(user))
			return "Cannot remove "
					+ user.getCompleteUserName()
					+ " as this user is the only administrator for this university";

		new UserRolesDAO(EduShareSession.get().getSessionORMInterface())
				.removeUser(univModel.getObject(), user);

		String message = "The user " + user.getCompleteUserName()
				+ " has been removed from" + " the university "
				+ univModel.getObject().getUniversityName();
		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new UnivDataHandler(univModel).getAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				univModel.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}

	@Override
	public String getRole(Users user) {
		Integer role = UnivRoleCheck.getRole(univModel.getObject(), user);
		Iterator<String> it = roleMap.keySet().iterator();
		while (it.hasNext()) {
			String s = it.next();
			if (roleMap.get(s).compareTo(role)==0) {
				return s;
			}
		}
		return "";
	}

	@Override
	public boolean isAdmin() {
		Users user = EduShareSession.get().getLoggedInUser();
		return (UnivRoleCheck.isAdmin(univModel.getObject(), user));
	}

	private HashMap<String, Integer> roleMap = null;
	private IModel<Universities> univModel = null;

	@Override
	public boolean isInvited(Users user) {
		List<UserRoles> roleList = user.getRoleList();
		for (UserRoles r : roleList) {
			if (r.getUniversity() == univModel.getObject()
					&& r.getStatus() == UserRoles.STATUS_INVITED)
				return true;
		}
		return false;
	}

	@Override
	public boolean isLastAdmin(Users user) {
		if(
				roleMap.get(getRole(user))!=null &&
				roleMap.get(getRole(user)).equals(UserRoles.ADMIN_ROLE) && 
				new UnivDataHandler(univModel).getAdmins().getObject().size()<2
		)
		{
			return true;
		}
		return false;
	}

}
